import { Quaternion, TransformNode, Vector3 } from '@babylonjs/core';
import { Stage } from 'babylon-lib/base/stage';
import { CreateUINode } from 'babylon-lib/gui/base/ui-node';
import { ANCHOR } from 'babylon-lib/gui/control/abstract-base';
import { CreateUIRectangle } from 'babylon-lib/gui/control/ui-rectangle';
import { CreateUIText } from 'babylon-lib/gui/control/ui-text';


interface ICharacterHeadLabelOptions {
    name: string;
}


class CharacterHeadLabel {

    public readonly container: TransformNode;

    private _stage: Stage;
    private _options: ICharacterHeadLabelOptions;

    constructor(stage: Stage, options: ICharacterHeadLabelOptions) {
        this._stage = stage;
        this._options = options;
        this.container = new TransformNode(this._options.name + 'HeadLabelContainer', this._stage.mainScene);
        this.initializeNode();
    }

    private initializeNode(): void {
        const name = this._options.name;
        const node = CreateUINode(name + 'UiNode', this._stage.mainScene, {
            anchor: new Vector3(0.5, 0, 0),
            lockToCamera: false,
            billboard: TransformNode.BILLBOARDMODE_ALL,
            rotation: Quaternion.FromEulerAngles(0, Math.PI, 0),
        }, [
            CreateUIRectangle(name + 'background', this._stage.mainScene, {
                anchor: ANCHOR.CENTER_BOTTOM,
                width: 10,
                height: 2,
                alpha: 0.3,
                cornerRadius: 100,
                background: '#00aaff',
                inoperable: true,
            }),
            CreateUIText(name + 'NameText', this._stage.mainScene, {
                anchor: ANCHOR.CENTER_BOTTOM,
                text: this._options.name,
                canvasSize: 32,
                fontSize: 36,
                fontColor: '#ffffff',
                width: 6,
                height: 2,
                inoperable: true,
            })
        ]);
        node.root.parent = this.container;
        node.root.position.set(0, 0, 0);
    }


}


export { CharacterHeadLabel };